<?php
!function_exists('readover') && exit('Forbidden');

Class DB {

	var $c_id=0;
	var $query_num = 0;

	function DB($dbhost, $dbuser, $dbpw, $dbname, $pconnect = 0) {
		$this->connect($dbhost, $dbuser, $dbpw, $dbname, $pconnect);
	}

	function connect($dbhost, $dbuser, $dbpw, $dbname, $pconnect = 0) {
		$mysql_connect = $pconnect ? 'mysql_pconnect' : 'mysql_connect' ;
		$this->c_id=@$mysql_connect($dbhost, $dbuser, $dbpw);
		if(!$this->c_id) {
			$this->halt("Connect to MySQL ($dbhost,$dbuser) failed");
		}
		if($dbname) {
		  if (!@mysql_select_db($dbname,$this->c_id)){
			$this->halt('Cannot use database '.$dbname);
		  }
		}
	}

	function close() {
		return mysql_close($this->c_id);
	}

	function query($SQL) {
		$GLOBALS['PW']=='pw_' or $SQL=str_replace('pw_',$GLOBALS['PW'],$SQL);
		$query =& mysql_query($SQL,$this->c_id);
		$this->query_num++;
		if (!$query)  $this->halt('Query Error: ' . $SQL);
		return $query;
	}

	function get_one($SQL){
		$rs =& mysql_fetch_array($this->query($SQL));
		return $rs;
	}

	function update($SQL,$method='U_B') {
		$GLOBALS['PW']=='pw_' or $SQL=str_replace('pw_',$GLOBALS['PW'],$SQL);
		if($method=='U_B' && function_exists('mysql_unbuffered_query')){
			$mysql_query='mysql_unbuffered_query';
		}else{
			$mysql_query='mysql_query';
		}
		//$SQL=substr($SQL,0,6).' LOW_PRIORITY'.substr($SQL,6);
		$query = $mysql_query($SQL,$this->c_id);
		$this->query_num++;
		if (!$query)  $this->halt('Update Error: ' . $SQL);
		return $query;
	}

	function fetch_array($query, $result_type = MYSQL_ASSOC) {
		return mysql_fetch_array($query, $result_type);
	}

	function affected_rows() {
		return mysql_affected_rows($this->c_id);
	}

	function result($query, $row) {
		$query = mysql_result($query, $row);
		return $query;
	}

	function num_rows($query) {
		$rows = mysql_num_rows($query);
		return $rows;
	}

	function free_result($query) {
		return mysql_free_result($query);
	}

	function insert_id() {
		$id = mysql_insert_id($this->c_id);
		return $id;
	}

	function halt($msg='') {
		include_once('./require/db_mysql_error.php');
		new DB_ERROR($msg,$this->c_id);
	}
}
?>